package com.sywb.chuangyebao.contract;

import android.app.Activity;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.NewsInfo;
import com.sywb.chuangyebao.bean.SuperInfo;
import com.sywb.chuangyebao.library.view.RatioImageView;
import com.sywb.chuangyebao.utils.GlideUtils;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.view.NewsDetailActivity;
import com.sywb.chuangyebao.view.WebActivity;

import org.bining.footstone.adapter.BaseRecyclerMultiItemAdapter;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.log.Logger;
import org.bining.footstone.utils.TimeUtils;

import java.util.List;

public interface NewsContract {

    interface View extends BaseRefreshContract.View {
        int getUId();
    }

    class Presenter extends BaseRefreshContract.Presenter<View> {

        protected NewsAdapter recyclerAdapter;

        private int uId;
        private int pageNumber = 1;

        @Override
        public void onStart() {
            super.onStart();
            uId = mView.getUId();

            recyclerAdapter = new NewsAdapter(mActivity, uId);
            setRecyclerAdapter(recyclerAdapter);
            if (uId > 0 && uId != 1234567) {
                pullToRefreshView.setRefreshEnable(false);
                recyclerAdapter.setPageSize(1);
            }

            initData();
        }

        @Override
        public void initData() {
            onStartAsync();
            getArticleList();
        }

        @Override
        public void onStartRefresh() {
            pageNumber = 1;
            getArticleList();
        }

        @Override
        public void onStartLoadMore() {
            pageNumber++;
            getArticleList();
        }

        private void getArticleList() {
            HttpUtils.getContentList(Constants.TOPIC_TYPE_ARTICLE, uId < 0 ? null : String.valueOf(uId), null, pageNumber, null, null, null, new HttpCallback<List<SuperInfo>>() {
                @Override
                public void onSuccess(List<SuperInfo> superInfos) {
                    for (SuperInfo superInfo : superInfos) {
                        superInfo.setItemType(superInfo.item_style);
                    }
                    if (pageNumber == 1) {
                        onEndRefresh();
//                        if (superInfos != null && superInfos.size() > 0) {
//                            recyclerAdapter.setFooterView(R.layout.layout_footer);
//                        } else {
//                            recyclerAdapter.setFooterView(null);
//                        }
                        if (uId > 0 && uId != 1234567) {
                            recyclerAdapter.setFooterView(null);
                        } else {
                            if (superInfos != null && superInfos.size() > 0) {
                                recyclerAdapter.setFooterView(R.layout.layout_footer);
                            } else {
                                recyclerAdapter.setFooterView(null);
                            }
                        }
                        recyclerAdapter.clearDatas();
                    } else if (uId > 0 && uId != 1234567) {
                        if (superInfos != null && superInfos.size() > 0) {
                            recyclerAdapter.setFooterView(R.layout.layout_footer);
                        } else {
                            recyclerAdapter.setFooterView(null);
                        }
                    }
                    recyclerAdapter.notifyDataChangedAfterLoadMore(superInfos);
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    if (pageNumber == 1) {
                        onEndRefresh();
                    } else {
                        onEndLoadMore();
                    }
                    showMessage(strMsg);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                    isNoData();
                }
            });
        }

        @Override
        public void onRecyclerItemClick(int i) {
            SuperInfo superInfo = recyclerAdapter.getItem(i);
            NewsInfo newsInfo = JSON.parseObject(superInfo.content, NewsInfo.class);
            if (!newsInfo.is_ad) {
                newsInfo.hits += 1;
                newsInfo.count.hits += 1;
                superInfo.content = JSON.toJSONString(newsInfo);
                recyclerAdapter.notifyDataSetChanged();
                mView.advance(NewsDetailActivity.class, newsInfo.item_id);
            } else {
                mView.advance(WebActivity.class, "", newsInfo.url);
            }
        }

    }

    class NewsAdapter extends BaseRecyclerMultiItemAdapter<SuperInfo> {

        private Activity mActivity;
        private int newsUid;

        public NewsAdapter(Activity activity, int newsUid) {
            super(activity, null);
            this.mActivity = activity;
            this.newsUid = newsUid;
            addItemType(Constants.NEWS_TEXT, R.layout.item_news_text);//纯文字布局
            addItemType(Constants.NEWS_CENTER_SINGLE_PIC, R.layout.item_news_center_pic);//居中大图布局
            addItemType(Constants.NEWS_RIGHT_PIC_VIDEO, R.layout.item_news_right_pic);//右侧小图布局
            addItemType(Constants.NEWS_THREE_PICS, R.layout.item_news_three_pics);//三张图片布局
        }

        @Override
        protected int getDefItemViewType(int position) {
            if (position >= getDataCount()) {
                return 0x00000333;
            }
            return super.getDefItemViewType(position);
        }

        @Override
        protected void setMultiData(ViewHolderHelper viewHolderHelper, int i, SuperInfo superInfo) {

            NewsInfo newsInfo = JSON.parseObject(superInfo.content, NewsInfo.class);

            if (TextUtils.isEmpty(newsInfo.title)) {
                return;
            }

            viewHolderHelper.setVisibility(R.id.tv_author, newsUid == -1);

            if (newsInfo.owner != null) {
                viewHolderHelper.setText(R.id.tv_author, newsInfo.owner.getShowName());
            } else {
                viewHolderHelper.setText(R.id.tv_author, newsInfo.writer);
            }

            viewHolderHelper.setText(R.id.tv_title, newsInfo.title)
                    .setText(R.id.tv_time, TimeUtils.transformDate10(newsInfo.publish_time));

            viewHolderHelper.setVisibility(R.id.tv_tag, android.view.View.GONE);

            if (newsInfo.is_ad) {
                viewHolderHelper.setVisibility(R.id.tv_comment_num, android.view.View.GONE);
                viewHolderHelper.setVisibility(R.id.tv_tag, android.view.View.VISIBLE).setText(R.id.tv_tag, "广告");
            } else {
                viewHolderHelper.setVisibility(R.id.tv_tag, android.view.View.GONE);
                viewHolderHelper.setVisibility(R.id.tv_comment_num, android.view.View.VISIBLE);
                viewHolderHelper.setText(R.id.tv_comment_num, newsInfo.count.getHitsString());
            }

            switch (superInfo.getItemType()) {
                case Constants.NEWS_THREE_PICS:
                    RatioImageView iv_img1 = viewHolderHelper.getView(R.id.iv_img1);
                    RatioImageView iv_img2 = viewHolderHelper.getView(R.id.iv_img2);
                    RatioImageView iv_img3 = viewHolderHelper.getView(R.id.iv_img3);

                    iv_img1.setVisibility(android.view.View.INVISIBLE);
                    iv_img2.setVisibility(android.view.View.INVISIBLE);
                    iv_img3.setVisibility(android.view.View.INVISIBLE);

                    if (newsInfo.thumbnails != null && newsInfo.thumbnails.size() > 0) {
                        for (int j = 0; j < newsInfo.thumbnails.size(); j++) {
                            String thumbnail = newsInfo.thumbnails.get(j).trim();
                            Logger.e("thumbnail:" + thumbnail);
                            if (!TextUtils.isEmpty(thumbnail)) {
                                switch (j) {
                                    case 0:
                                        iv_img1.setVisibility(android.view.View.VISIBLE);
                                        GlideUtils.getNormalIamgeToUrl(mActivity, thumbnail, iv_img1, R.drawable.news_pic_default);
                                        break;
                                    case 1:
                                        iv_img2.setVisibility(android.view.View.VISIBLE);
                                        GlideUtils.getNormalIamgeToUrl(mActivity, thumbnail, iv_img2, R.drawable.news_pic_default);
                                        break;
                                    case 2:
                                        iv_img3.setVisibility(android.view.View.VISIBLE);
                                        GlideUtils.getNormalIamgeToUrl(mActivity, thumbnail, iv_img3, R.drawable.news_pic_default);
                                        break;
                                }
                            }
                        }
                    }
                    break;
                case Constants.NEWS_RIGHT_PIC_VIDEO:
                    RatioImageView iv_img_right = viewHolderHelper.getView(R.id.iv_img);
                    iv_img_right.setImageResource(R.drawable.news_pic_default);
                    if (newsInfo.thumbnails != null && newsInfo.thumbnails.size() > 0 && !TextUtils.isEmpty(newsInfo.thumbnails.get(0).trim())) {
                        GlideUtils.getNormalIamgeToUrl(mActivity, newsInfo.thumbnails.get(0), iv_img_right, R.drawable.news_pic_default);
                    }
                    viewHolderHelper.setVisibility(R.id.tv_duration, android.view.View.GONE);
                    break;
                case Constants.NEWS_CENTER_SINGLE_PIC:
                    RatioImageView iv_img_center = viewHolderHelper.getView(R.id.iv_img);
                    iv_img_center.setImageResource(R.drawable.news_pic_default);
                    if (newsInfo.thumbnails != null && newsInfo.thumbnails.size() > 0 && !TextUtils.isEmpty(newsInfo.thumbnails.get(0).trim())) {
                        GlideUtils.getNormalIamgeToUrl(mActivity, newsInfo.thumbnails.get(0), iv_img_center, R.drawable.news_pic_default);
                    }
                    viewHolderHelper.setVisibility(R.id.tv_duration, android.view.View.GONE);
                    break;
            }
        }
    }
}

